﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Models;
using ChinaDynastyTours.Controls;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class HotelOrderForm : DevExpress.XtraEditors.XtraForm
    {
        //输入参数
        public string _TourGroupID { get; set; }//团ID
        public string _TourGroupState { get; set; }//团状态
        public int _TourGroupScheduleID { get; set; }//行程计划ID
        public DateTime _ArrivalTime { get; set; }//抵达时间
        public int _StayOver { get; set; }//过夜数


        private string _desCityName;//目标城市名称

        private int _ExpenseID;//费用ID
        private int _HotelOrderID;//用于判定当前选定的行程计划是否存在宾馆预定
        private DataTable _dtHotelOrderData;
        //输出参数
        public string _ReturnHotelID { get; set; }
        public string _ReturnHotelName { get; set; }
        public string _ReturnScheduleMethod { get; set; }

        public HotelOrderForm(string desCityName)
        {
            InitializeComponent();

            _desCityName = desCityName;
        }

        #region 初始化
        private void HotelOrderForm_Load(object sender, EventArgs e)
        {
            lblTourGroupState.Text = _TourGroupState;
            lblTourGroupID.Text = _TourGroupID;

            cbScheduleMethod.Properties.Items.AddRange(Enum.GetNames(typeof(HotelScheduleMethodEnum)));
            cbScheduleMethod.SelectedIndex=0;

            _dtHotelOrderData = HotelOrderCtrl.GetHotelOrderData(_TourGroupScheduleID);

            InitializeGUIData();
        }
        private void InitializeGUIData()
        {
            if (_dtHotelOrderData != null && _dtHotelOrderData.Rows.Count > 0)
            {
                //与UI绑定
                //lblProviderID.DataBindings.Add("Text", _dtHotelOrderData, "ProviderID");
                cbScheduleMethod.DataBindings.Add("EditValue",_dtHotelOrderData, "ScheduleMethod");
                tbProviderName.DataBindings.Add("Tag", _dtHotelOrderData, "ProviderID");
                tbProviderName.DataBindings.Add("Text", _dtHotelOrderData, "ProviderName");
                cbBreakMethod.DataBindings.Add("EditValue", _dtHotelOrderData, "BreakMethod");
                tbArrivalTime.DataBindings.Add("EditValue", _dtHotelOrderData, "ArrivalTime");
                tbLeaveTime.DataBindings.Add("EditValue", _dtHotelOrderData, "LeaveTime");
                tbStayOver.DataBindings.Add("EditValue", _dtHotelOrderData, "StayOver");
                tbSingleRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "SingleRoomNum");
                tbDoubleRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "DoubleRoomNum");
                tbThreeRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "ThreeRoomNum");
                tbBigBedRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "BigBedRoomNum");
                tbAccompanyRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "AccompanyRoomNum");
                //tbAppendRoomNum.DataBindings.Add("EditValue", _dtHotelOrderData, "AppendRoomNum");
                tbSpecialRoom.DataBindings.Add("EditValue", _dtHotelOrderData, "SpecialRoom");
                tbBudget.DataBindings.Add("EditValue", _dtHotelOrderData, "Budget");

                //lblProviderID.DataBindings.Add("Text", _dtHotelOrderData, "ProviderID");
                lblContractPath.DataBindings.Add("Text", _dtHotelOrderData, "ContractPath");

                //取得费用标识
                _ExpenseID = int.Parse(_dtHotelOrderData.Rows[0]["ExpenseID"].ToString());

                _HotelOrderID = int.Parse(_dtHotelOrderData.Rows[0]["HotelOrderID"].ToString()); ;
            }
            else
            {
                _ExpenseID = -1;

                _HotelOrderID = -1;

                //如没有预订数据，将显示行程计划中的日期及过夜数
                tbArrivalTime.EditValue = _ArrivalTime;
                tbStayOver.EditValue = _StayOver;
                tbLeaveTime.EditValue = tbArrivalTime.DateTime.AddDays(_StayOver);

                //如没有预订数据，则从此团第一条宾馆预订中获取各种‘房型数量’进行填充，以省去过手工麻烦
                DataTable tmpDt = HotelOrderCtrl.GetFirstHotelOrderRoomNumForTourgroup(_TourGroupID);
                if (tmpDt.Rows.Count == 0) return;

                tbSingleRoomNum.EditValue = tmpDt.Rows[0]["SingleRoomNum"].ToString();
                tbDoubleRoomNum.EditValue = tmpDt.Rows[0]["DoubleRoomNum"].ToString();
                tbThreeRoomNum.EditValue = tmpDt.Rows[0]["ThreeRoomNum"].ToString();
                tbBigBedRoomNum.EditValue = tmpDt.Rows[0]["BigBedRoomNum"].ToString();
                tbAccompanyRoomNum.EditValue = tmpDt.Rows[0]["AccompanyRoomNum"].ToString();
            }
        } 
        #endregion

        /// <summary>
        /// 起止日期变更
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbEndDate_EditValueChanged(object sender, EventArgs e)
        {
            if (tbArrivalTime.EditValue == null || tbLeaveTime.EditValue == null)
                return;

            tbStayOver.EditValue = (tbLeaveTime.DateTime - tbArrivalTime.DateTime).Days;

            if ((tbLeaveTime.DateTime - tbArrivalTime.DateTime).Days < 0)
                toolTipCtrl.ShowHint("请注意：  起始日期大于结束日期", sender as Control, DevExpress.Utils.ToolTipLocation.RightBottom);
            else
                toolTipCtrl.HideHint();
        }
        //选择宾馆供应商
        private void btnHotel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            HotelOrderServiceForm hotelServiceForm = new HotelOrderServiceForm(_desCityName);
            if (_HotelOrderID != -1) //如存在预订的宾馆供应商，则将供应商ID传进去，以便定位
            {
                hotelServiceForm._ProviderID = tbProviderName.Tag.ToString();// lblProviderID.Text;
            }
            if (hotelServiceForm.ShowDialog(this) == DialogResult.OK)
            {
                DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
                btnEdit.EditValue = hotelServiceForm.ReturnProviderName;
                btnEdit.Tag = hotelServiceForm.ReturnProviderId;
                //lblProviderID.Text = hotelServiceForm.ReturnProviderId;
                tbSpecialRoom.Text = hotelServiceForm.ReturnServiceName;
                //lblContractPath.Text = hotelServiceForm.ReturnContractPath;
            }

        }

        #region '确定'按钮保存数据

        private void btnOk_Click(object sender, EventArgs e)
        {
            //if(cbScheduleMethod.SelectedIndex!=0)
            //{
            //    if(tbProviderName1.Text.Trim()=="")
            //    {
            //        MessageBox.Show("您还没有填写宾馆名称", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //        return;
            //    }
            //    //返回“宾馆+预定方式”
            //    ReturnHotelID = "-1";
            //    ReturnHotelName = tbProviderName1.Text;
            //    ReturnScheduleMethod = cbScheduleMethod.Text;
            //    DialogResult = DialogResult.OK;
            //    return;
            //}

            if (tbProviderName.Tag == null)
            {
                MessageBox.Show("您还没有选择宾馆名称", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //创建宾馆预定信息内容
            HotelOrderEntity hotelOrder = new HotelOrderEntity()
            {
                AccompanyRoomNum = tbAccompanyRoomNum.Text == "" ? 0 : int.Parse(tbAccompanyRoomNum.Text),
                ArrivalTime = tbArrivalTime.DateTime.ToString("yyyy-MM-dd"),
                BigBedRoomNum = tbBigBedRoomNum.Text == "" ? 0 : int.Parse(tbBigBedRoomNum.Text),
                BreakMethod = cbBreakMethod.Text,
                ContractPath = lblContractPath.Text,
                DoubleRoomNum = tbDoubleRoomNum.Text == "" ? 0 : int.Parse(tbDoubleRoomNum.Text),
                ExpenseID = _ExpenseID,
                HotelOrderID = _HotelOrderID,
                ProviderID = int.Parse(tbProviderName.Tag.ToString()),//int.Parse(lblProviderID.Text),
                ProviderName = tbProviderName.Text,
                LeaveTime = tbLeaveTime.DateTime.ToString("yyyy-MM-dd"),
                ScheduleMethod = cbScheduleMethod.Text,
                SingleRoomNum = tbSingleRoomNum.Text == "" ? 0 : int.Parse(tbSingleRoomNum.Text),
                SpecialRoom = tbSpecialRoom.Text,
                ThreeRoomNum = tbThreeRoomNum.Text == "" ? 0 : int.Parse(tbThreeRoomNum.Text),
                TourgroupID = _TourGroupID,
                TourGroupScheduleID = _TourGroupScheduleID,
                StayOver = tbStayOver.Text == "" ? 0 : int.Parse(tbStayOver.Text),
                OrderState= OrderStateEnum.未订.ToString()
            };

            if (_HotelOrderID == -1) //新建内容，无对应宾馆预定及相关费用信息
            {
                if (cbScheduleMethod.Text == HotelScheduleMethodEnum.自订.ToString())//自订，创建费用
                {
                    //创建费用
                    ExpenseEntity expense = new ExpenseEntity()
                    {
                        //ExpenseID = _ExpenseID,
                        TourgroupID = _TourGroupID,
                        TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),
                        ProviderID = int.Parse(tbProviderName.Tag.ToString()),//int.Parse(lblProviderID.Text),
                        ProviderName = tbProviderName.Text,
                        ServiceName = tbProviderName.Text,
                        Budget = tbBudget.EditValue.ToString() == "" ? 0 : decimal.Parse(tbBudget.EditValue.ToString()),
                        PayTotal = 0,
                        ApplyPayTotal = 0,
                        ApplyPay = 0,
                        PayStatus = PayApplyStatusEnum.未付.ToString(),
                        AdjustAmount = 0,
                        Status = ExpenseStatusEnum.计划.ToString(),
                        TypeBelong = ProviderTypeEnum.宾馆.ToString(),
                        CreateByUserName = Thread.CurrentPrincipal.Identity.Name, //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
                        TourGroupScheduleID = _TourGroupScheduleID,
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                        ExpenseTypeID = 1,//所属费用类型(费用类型表中存储着编号，此编号是固定的，1房费、2团费、3餐费、4交通费、5门票、6杂费)
                        ExpenseType = BaseInfoCtrl.GetExpenseTypeNameByID(1)//同上
                    };
                    HotelOrderCtrl.CreateHotelOrderAndExpenseData(expense, hotelOrder);
                }
                else//代订和确认，费用为取消状态
                {
                    //创建费用
                    ExpenseEntity expense = new ExpenseEntity()
                    {
                        //ExpenseID = _ExpenseID,
                        TourgroupID = _TourGroupID,
                        TourgroupName = TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),
                        ProviderID = int.Parse(tbProviderName.Tag.ToString()),//int.Parse(lblProviderID.Text),
                        ProviderName = tbProviderName.Text,
                        ServiceName = tbProviderName.Text,
                        Budget = tbBudget.EditValue.ToString() == "" ? 0 : decimal.Parse(tbBudget.EditValue.ToString()),
                        PayTotal = 0,
                        ApplyPayTotal = 0,
                        ApplyPay = 0,
                        PayStatus = PayApplyStatusEnum.未付.ToString(),
                        AdjustAmount = 0,
                        Status = ExpenseStatusEnum.取消.ToString(),
                        TypeBelong = ProviderTypeEnum.宾馆.ToString(),
                        CreateByUserName = Thread.CurrentPrincipal.Identity.Name, //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
                        TourGroupScheduleID = _TourGroupScheduleID,
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd"),
                        ExpenseTypeID = 1,//所属费用类型(费用类型表中存储着编号，此编号是固定的，1房费、2团费、3餐费、4交通费、5门票、6杂费)
                        ExpenseType = BaseInfoCtrl.GetExpenseTypeNameByID(1)//同上
                    };
                    HotelOrderCtrl.CreateHotelOrderAndExpenseData(expense, hotelOrder);
                }
            }
            else //更新内容
            {
                if (cbScheduleMethod.Text == HotelScheduleMethodEnum.自订.ToString())//自订
                {
                    ExpenseEntity expense = new ExpenseEntity()
                    {
                        ExpenseID = _ExpenseID, /*重点字段*/
                        ProviderID = int.Parse(tbProviderName.Tag.ToString()),//int.Parse(lblProviderID.Text),
                        ProviderName = tbProviderName.Text,
                        Budget = decimal.Parse(tbBudget.EditValue.ToString()),
                        //TypeBelong = ProviderTypeEnum.宾馆.ToString()
                        Status = ExpenseStatusEnum.计划.ToString(),
                        ServiceName = tbProviderName.Text
                    };
                    HotelOrderCtrl.UpdateHotelOrderandExpenseData(expense, hotelOrder);
                    //存储宾馆实体数据到数据库

                }
                else//代订、确认，费用状态为取消
                {
                    ExpenseEntity expense = new ExpenseEntity()
                    {
                        ExpenseID = _ExpenseID, /*重点字段*/
                        ProviderID = int.Parse(tbProviderName.Tag.ToString()),//int.Parse(lblProviderID.Text),
                        ProviderName = tbProviderName.Text,
                        Budget = decimal.Parse(tbBudget.EditValue.ToString()),
                        //TypeBelong = ProviderTypeEnum.宾馆.ToString()
                        Status = ExpenseStatusEnum.取消.ToString(),
                        ServiceName = tbProviderName.Text
                    };
                    HotelOrderCtrl.UpdateHotelOrderandExpenseData(expense, hotelOrder);
                    //存储宾馆实体数据到数据库
                }
            }
            //更新团审核成本
            TeamGroupCtrl.UpdateTourGroupBudgeCost(_TourGroupID);
            
            //返回“宾馆+预定方式”
            _ReturnHotelID = tbProviderName.Tag.ToString();// lblProviderID.Text;
            _ReturnHotelName = tbProviderName.Text;
            _ReturnScheduleMethod = cbScheduleMethod.Text;
            DialogResult = DialogResult.OK;
        } 
        #endregion

        //private void cbScheduleMethod_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    switch (cbScheduleMethod.SelectedIndex)
        //    {
        //        case 0:
        //            tbProviderName.Enabled = true;
        //            tbProviderName1.Enabled = false;
        //            break;
        //        case 1:
        //        case 2:
        //            tbProviderName.Enabled = false;
        //            tbProviderName1.Enabled = true;
        //            break;
        //        default: break;
        //    }
        //}

    }
}
